<template>
  <div>
    <Hearder />
    <el-card>
      <el-alert
        title="提示:只能看见三级分支"
        :closable="false"
        show-icon
        type="warning"
      >
      </el-alert>
      <el-row class="xx">
        <el-col>
          <span>选择商品分类:</span>
          <el-cascader
            v-model="slectList"
            :options="cascadeList"
            :props="cascade"
            @change="handleChange"
          >
          </el-cascader>
        </el-col>
      </el-row>

      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="only">
          <el-button type="danger" :disabled="istabs" @click="btnattribute(1)"
            >动态</el-button
          >

          <el-table :data="dynamicOnly" border stripe>
            <el-table-column type="expand"> </el-table-column>
            <el-table-column type="index" label="ID"> </el-table-column>
            <el-table-column prop="attr_name" label="家电类"> </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="btnlist(1, scope.row.attr_id)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="many">
          <el-button type="danger" :disabled="istabs" @click="btnattribute(1)"
            >静态属性</el-button
          >

          <el-table :data="static" border stripe>
            <el-table-column type="expand"> </el-table-column>
            <el-table-column type="index" label="ID"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  @click="btnlist(1, scope.row.attr_id)"
                  >编辑</el-button
                >
                <el-button type="danger" icon="el-icon-delete">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <el-dialog
      :title="isType"
      :visible.sync="dialogVisible"
      width="30%"
      @close="clearInput"
    >
      <el-form
        :model="formdataDynamic"
        :rules="fromRules"
        ref="ruleForm"
        v-if="!this.fromDataItem.isList"
      >
        <el-form-item
          :label="'添加' + isType"
          prop="sataic"
          label-width="120px"
        >
          <el-input v-model="formdataDynamic.sataic"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="fromDataItem" ref="ruleForm" v-else>
        <el-form-item label="编辑商品参数" prop="sataic" label-width="120px">
          <el-input v-model="fromDataItem.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteBtn">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fromDataItem: [],
      formdataDynamic: {
        sataic: "",
      },
      fromRules: {
        sataic: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      dialogVisible: false,
      dynamicOnly: [],
      static: [],
      activeName: "only",
      slectList: [],
      cascade: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      cascadeList: [],
      getInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
    };
  },
  methods: {
    async btnlist(list, id) {
      this.dialogVisible = true;
      if (list) {
        const { data: res } = await this.$http
          .get(`categories/${this.tabsId}/attributes/${id}`, {
            params: { attr_sel: this.activeName },
          })
          .catch((err) => err);
        this.fromDataItem = res.data;
        this.fromDataItem.isList = 1;
        console.log(9999, this.fromDataItem);
      } else {
        console.log(222);
      }
      this.getTabList();
    },
    deleteBtn() {
      console.log(12312, this.cid);
      if (this.fromDataItem.isList) {
        this.$refs.ruleForm.validate(async (item) => {
          console.log(item);
          if (!item) return;
          const { data: res } = await this.$http
            .put(
              `categories/${this.tabsId}/attributes/${this.fromDataItem.attr_id}`,
              {
                attr_name: this.fromDataItem.attr_name,
                attr_sel: this.activeName,
              }
            )
            .catch((err) => err);
          console.log(6666555, res);
            this.this.fromDataItem.attr_name=''
          this.getTabList();
        });
      
      }

      this.$refs.ruleForm.validate(async (item) => {
        console.log(item);
        if (!item) return;
        const { data: res } = await this.$http
          .post(`categories/${this.tabsId}/attributes`, {
            attr_name: this.formdataDynamic.sataic,
            attr_sel: this.activeName,
          })
          .catch((err) => err);
        console.log(res);
        this.getTabList();
      });

      this.dialogVisible = false;
    },
    clearInput() {
      this.$refs.ruleForm.resetFields();
    },
    btnattribute(list) {
      if (list) {
        this.btnlist();
      }
      this.dialogVisible = true;
    },
    handleClick() {
      this.getTabList();
    },
    handleChange() {
      console.log(111666, this.cascadeList);
      this.getTabList();
    },
    async getTabList() {
      const { data: res } = await this.$http
        .get(`categories/${this.tabsId}/attributes`, {
          params: { sel: this.activeName },
        })
        .catch((err) => err);
      console.log(666, res.data);
      if (this.activeName === "only") {
        this.dynamicOnly = res.data;
      } else {
        this.static = res.data;
      }
    },
    async getcascadeList() {
      const { data: res } = await this.$http
        .get("categories", { params: this.getInfo })
        .catch((err) => err);
      this.cascadeList = res.data.result;
      console.log(111111, res.data, this.slectList);
    },
  },
  computed: {
    istabs() {
      if (this.slectList.length !== 3) {
        return true;
      } else {
        return false;
      }
    },
    tabsId() {
      if (this.slectList.length === 3) {
        return this.slectList[2];
      } else {
        return null;
      }
    },
    isType() {
      if (this.activeName === "only") {
        return "动态参数";
      } else {
        return "静态属性";
      }
    },
  },
  mounted() {
    this.getcascadeList();
  },
};
</script>

<style>
.xx {
  margin: 10px;
}
</style>
